<?php

/**
 * @author Orhan POLAT
 * @author Murat Cem YALIN <muratcem@gmail.com>
 * @copyright 2014 muratcem.com - walkerPHP.com
 * @license http://opensource.org/licenses/GPL-3.0 GNU General Public License, version 3
 * @version 1.0.1
 */

if (!function_exists('value')){
    /**
     * returns default value of the $value
     * @author Orhan Polat
     *
     * @param mixed $value
     * @return mixed
     */
    function value($value){
        return $value instanceof Closure ? $value() : $value;
    }
}

if (!function_exists('object_get')){
    /**
     * gets value from an object using "dot" notation
     * @author Orhan Polat
     *
     * @param object $object
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    function object_get($object, $key, $default = NULL){
        if (is_null($key)){
            return $default;
        }

        foreach(explode('.', $key) as $segment){
            if (!is_object($object) || !isset($object->{$segment})){
                return value($default);
            }

            $object = $object->{$segment};
        }
        return $object;
    }
}

if (!function_exists('array_get')){
    /**
     * gets value from an array using "dot" notation
     * @author Orhan Polat
     *
     * @param array|object $array
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    function array_get($array, $key, $default = NULL){
        if (is_object($array)){
            return object_get($array, $key, $default);
        }

        if (is_null($key)){
            return $default;
        }

        if (is_array($array) && isset($array[$key])){
            return $array[$key];
        }

        foreach(explode('.', $key) as $segment){
            if (!is_array($array) || !array_key_exists($segment, $array)){
                return value($default);
            }

            $array = $array[$segment];
        }
        return $array;
    }
}


if (!function_exists('array_set')){
    /**
     * Set an array item to a given value using "dot" notation.
     * If key is defined as an empty string, the entire array will be replaced.
     * @author Orhan Polat
     *
     * @param array $array
     * @param string $key
     * @param mixed $value
     * @return void
     */
    function array_set(&$array, $key, $value) {
        if (!is_null($key)){
            $keys = explode('.', $key);
            while (count($keys) > 1){
                $key = array_shift($keys);
                if (!isset($array[$key]) || !is_array($array[$key])){
                    $array[$key] = array();
                }
                $array = &$array[$key];
            }
            $array[array_shift($keys)] = $value;
        }
    }
}

if (!function_exists("array_forget")){
    /**
     * Remove the $key from the given array using "dot" notation.
     * @author Orhan Polat
     *
     * @param array $array
     * @param string $key
     * @return void
     */
    function array_forget(&$array, $key) {
        $keys = explode('.', $key);
        while (count($keys) > 1){
            $key = array_shift($keys);
            if (!isset($array[$key]) || !is_array($array[$key])){
                return;
            }
            $array = &$array[$key];
        }
        unset($array[array_shift($keys)]);
    }
}

if (!function_exists("array_pull")){
    /**
     * Get a value from the array, and remove it.
     * @author Orhan Polat
     *
     * @param array $array
     * @param string $key
     * @return mixed
     */
    function array_pull(&$array, $key) {
        $value = array_get($array, $key);
        array_forget($array, $key);

        return $value;
    }
}

if (!function_exists("array_flatten")){
    /**
     * Flatten a multi-dimensional array into a single level.
     * @author Orhan Polat
     *
     * @param array $array
     * @return array
     */
    function array_flatten($array) {
        $return = array();
        array_walk_recursive($array, function ($x) use(&$return) {
            $return[] = $x;
        });

        return $return;
    }
}

if (!function_exists("array_dot")){
    /**
     * Flatten a multi-dimensional associative array with dots.
     * @author Orhan Polat
     *
     * @param array $array
     * @param string $prepend
     * @return array
     */
    function array_dot($array, $prepend = "") {
        $results = array();

        foreach ($array as $key => $value){
            if (is_array($value)){
                $results = array_merge($results, array_dot($value, $prepend.$key."."));
            }else{
                $results[$prepend.$key] = $value;
            }
        }

        return $results;
    }
}

if (!function_exists("array_except")){
    /**
     * Get all of the given array except for a specified array of items.
     * @author Orhan Polat
     *
     * @param array $array
     * @param array $keys
     * @return array
     */
    function array_except($array, $keys) {
        return array_diff_key($array, array_flip((array) $keys));
    }
}

if (!function_exists("array_fetch")){
    /**
     * Fetch a flattened array of a nested array element.
     * @author Orhan Polat
     *
     * @param array $array
     * @param string $key
     * @return array
     */
    function array_fetch($array, $key) {
        //Return
        $results = array();
        foreach (explode(".", $key) as $segment){
            foreach ($array as $value){
                $value = (array) $value;
                $results[] = $value[$segment];
            }
            $array = array_values($results);
        }
        return array_values($results);
    }
}
if (!function_exists("array_first")){
    /**
     * Get the first element of an array. Useful for method chaining.
     * @author Orhan Polat
     *
     * @param array $array
     * @return mixed
     */
    function array_first($array) {
        return reset($array);
    }
}
if (!function_exists("array_last")){
    /**
     * Get the last element from an array.
     * @author Orhan Polat
     *
     * @param array $array
     * @return mixed
     */
    function array_last($array) {
        return end($array);
    }
}
if (!function_exists("array_first_key")){
    /**
     * Array first key
     * @author Orhan Polat
     *
     * @param array $array
     * @param mixed $default
     * @return mixed
     */
    function array_first_key($array, $default = null) {
        //Return
        $first = $default;

        foreach ($array as $key => $value){
            $first = $key;
            break;
        }

        return $first;
    }
}
if (!function_exists("array_last_key")){
    /**
     * Array last key
     * @author Orhan Polat
     *
     * @param array $array
     * @param mixed $default
     * @return mixed
     */
    function array_last_key($array, $default = null) {
        //Return
        $last = $default;

        foreach ($array as $key => $value){
            $last = $key;
        }

        return $last;
    }
}


























